//
//  FCTabViewController.swift
//  miaoyin
//
//  Created by David on 2017/2/14.
//  Copyright © 2017年 David. All rights reserved.
//

import UIKit

class FCTabViewController: UITabBarController, UITabBarControllerDelegate, UIScrollViewDelegate, GetScanViewControllerDelegate, FileUploadDelegate, QQApiInterfaceDelegate {


    var bgView: UIVisualEffectView!
    
    var centerBtn: UIButton!
    
    var fileView: FilePreView?
    
    var firstLaunchScroll: UIScrollView?
//    var firstLaunchFinish = true
    
    var scanResultView: ScanResultView!
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        delegate = self
        
        tabBar.tintColor = UIColor.white
        tabBar.barTintColor = themeColor2
        tabBar.barStyle = .black
        
        bgView = UIVisualEffectView(frame: view.frame)
        
        bgView.effect = UIBlurEffect(style: .dark)
        bgView.alpha = 0
        view.addSubview(bgView)
        
        let btnClose = UIButton(frame: CGRect(x: 0, y: view.bounds.height - tabBar.frame.height + 2, width: tabBar.frame.height - 4, height: tabBar.frame.height - 4))
        btnClose.center.x = view.center.x
//        btnClose.setTitle("关闭", for: .normal)
//        btnClose.setTitleColor(UIColor.white, for: .normal)
//        btnClose.backgroundColor = themeColor1
        btnClose.setTitle("×", for: .normal)
        btnClose.setTitleColor(UIColor.white, for: .normal)
        btnClose.titleLabel?.font = UIFont.systemFont(ofSize: 36)
        bgView.addSubview(btnClose)
        btnClose.addTarget(self, action: #selector(FCTabViewController.toggleBGView), for: .touchUpInside)
        
        let btn1 = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
        btn1.tag = 348
//        btn1.backgroundColor = themeColor2
        btn1.setTitleColor(UIColor.white, for: .normal)
        btn1.setTitle("文件云库", for: .normal)
        btn1.setImage(#imageLiteral(resourceName: "doc_menu"), for: .normal)
        btn1.imageView?.contentMode = .scaleAspectFit
        btn1.imageEdgeInsets = UIEdgeInsets(top: -36, left: 16, bottom: 0, right: 0)
        btn1.titleLabel?.contentMode = .center
        btn1.titleEdgeInsets = UIEdgeInsets(top: 64, left: -44, bottom: 0, right: 0)
        bgView.addSubview(btn1)
        btn1.addTarget(self, action: #selector(FCTabViewController.gotoDocument), for: .touchUpInside)
        
        let btn2 = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
        btn2.tag = 452
//        btn2.backgroundColor = themeColor2
        btn2.setTitleColor(UIColor.white, for: .normal)
        btn2.setTitle("我的订单", for: .normal)
        btn2.setImage(#imageLiteral(resourceName: "order_menu"), for: .normal)
        btn2.imageView?.contentMode = .scaleAspectFit
        btn2.imageEdgeInsets = UIEdgeInsets(top: -36, left: 16, bottom: 0, right: 0)
        btn2.titleLabel?.contentMode = .center
        btn2.titleEdgeInsets = UIEdgeInsets(top: 64, left: -40, bottom: 0, right: 0)
        bgView.addSubview(btn2)
        btn2.addTarget(self, action: #selector(FCTabViewController.gotoOrder), for: .touchUpInside)
        
        let btn3 = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
        btn3.tag = 794
//        btn3.backgroundColor = themeColor2
        btn3.setTitleColor(UIColor.white, for: .normal)
        btn3.setTitle("取单扫码", for: .normal)
        btn3.setImage(#imageLiteral(resourceName: "scan_large_white"), for: .normal)
        btn3.imageView?.contentMode = .scaleAspectFit
        btn3.imageEdgeInsets = UIEdgeInsets(top: -36, left: 16, bottom: 0, right: 0)
        btn3.titleLabel?.contentMode = .center
        btn3.titleEdgeInsets = UIEdgeInsets(top: 64, left: -40, bottom: 0, right: 0)
        bgView.addSubview(btn3)
        btn3.addTarget(self, action: #selector(FCTabViewController.gotoScanCode), for: .touchUpInside)
        
        btn2.center = CGPoint(x: view.center.x, y: view.center.y)
        btn1.center = CGPoint(x: view.center.x, y: view.center.y - 132)
        btn3.center = CGPoint(x: view.center.x, y: view.center.y + 132)
        
        let modalView = UIView(frame: CGRect(x: 0, y: 0, width: tabBar.frame.height, height: tabBar.frame.height))
        modalView.center.x = view.center.x
        tabBar.addSubview(modalView)
        
        
        centerBtn = UIButton(frame: CGRect(x: 0, y: 2, width: tabBar.frame.height, height: tabBar.frame.height))
        centerBtn.center.x = view.center.x
//        centerBtn.setTitle("按钮", for: .normal)
//        centerBtn.setTitleColor(UIColor.white, for: .normal)
//        centerBtn.backgroundColor = themeColor2
        tabBar.addSubview(centerBtn)
        
//        let iconView = UIImageView(frame: CGRect(x: 0, y: 2, width: 28, height: 28))
//        iconView.center.x = 22
//        iconView.image = #imageLiteral(resourceName: "print_menu")
//        centerBtn.addSubview(iconView)
//        
//        let titleLabel = UILabel(frame: CGRect(x: 0, y: 36, width: 44, height: 8))
//        titleLabel.textColor = UIColor.white
//        titleLabel.text = "打印"
//        titleLabel.textAlignment = .center
//        titleLabel.font = UIFont.systemFont(ofSize: 10)
//        centerBtn.addSubview(titleLabel)
        
        centerBtn.addTarget(self, action: #selector(FCTabViewController.toggleBGView), for: .touchUpInside)
        
        if UserDefaults.standard.object(forKey: firstLaunchKey) == nil{
//            firstLaunchFinish = false
            firstLaunchScroll = UIScrollView(frame: view.frame)
            firstLaunchScroll?.delegate = self
            view.addSubview(firstLaunchScroll!)
            firstLaunchScroll?.contentSize = CGSize(width: view.frame.width * 4, height: view.frame.height)
            firstLaunchScroll?.isPagingEnabled = true
            firstLaunchScroll?.showsHorizontalScrollIndicator = false
            firstLaunchScroll?.bounces = false
            
            
            for i in 0..<3{
                let image = UIImageView(frame: CGRect(x: CGFloat(i)*view.frame.width-1, y: 0, width: view.frame.width+1, height: view.frame.height))
                image.image = UIImage(named: "st\(i+1)")
                firstLaunchScroll?.addSubview(image)
            }
        }
        
        scanResultView = ScanResultView(frame: view.frame)
        view.addSubview(scanResultView)
        
        fileView = FilePreView(frame: view.frame, vc: self)
        fileView!.uploadDelegate = self
        view.addSubview(fileView!)
//        openFile(url: URL(string: "file://\(DOCUMENT_PATH)/Inbox/file.doc"))
    }
    
    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        
        
    }
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        if scrollView.contentOffset.x > view.frame.width*2.4 {
            scrollView.alpha = 0
            scrollView.removeFromSuperview()
            firstLaunchScroll = nil
            UserDefaults.standard.set(true, forKey: firstLaunchKey)
            let firstVC = (UIApplication.shared.delegate as? AppDelegate)?.firstVC
            if firstVC != nil{
                firstVC?.refreshSpotPage()
            }
        }
    }
    
    func openFile(url: URL?){
        if fileView == nil{
            fileView = FilePreView(frame: view.frame, vc: self)
            fileView!.uploadDelegate = self
            view.addSubview(fileView!)
            
        }
        presentedViewController?.dismiss(animated: false, completion: nil)
        view.bringSubview(toFront: fileView!)
        fileView?.openFile(url: url)
    }
    
    func toggleBGView(){
        view.bringSubview(toFront: bgView)
        UIView.animate(withDuration: 0.3) { 
            self.bgView.alpha = self.bgView.alpha == 0 ? 1 : 0
        }
        
    }
    
    var spotIsOn = true
    
    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
        if viewController.isEqual(viewControllers![1]) {
            return false
        }
        if viewController.isEqual(viewControllers![0]){
            if spotIsOn {
                let firstVC = (UIApplication.shared.delegate as? AppDelegate)?.firstVC
                if firstVC != nil{
                    if firstVC!.tableView.contentOffset.y > -64.0{
                        firstVC?.tableView.setContentOffset(CGPoint(x: 0, y: -64), animated: true)
                    }
                }
            }
            spotIsOn = true
        }else{
            spotIsOn = false
        }
        return true
    }
    
    override func setViewControllers(_ viewControllers: [UIViewController]?, animated: Bool) {
        super.setViewControllers(viewControllers, animated: animated)
        tabBar.bringSubview(toFront: centerBtn)
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    func gotoDocument(){
//        present(UserEditViewController(), animated: true, completion: nil)
        if isLogin() {
            let nav = UINavigationController(rootViewController: LibraryViewController())
            present(nav, animated: true, completion: nil)
        }else{
            present(FCLoginViewController(), animated: true, completion: nil)
        }
        
    }
    
    func gotoOrder(){
        if isLogin(){
            let nav = UINavigationController(rootViewController: OrderListViewController())
            present(nav, animated: true, completion: nil)
        }else{
            present(FCLoginViewController(), animated: true, completion: nil)
        }
        
    }

    func gotoScanCode(){
        if isLogin() {
            let scanVC = GetScanViewController()
            scanVC.delegate = self
            present(scanVC, animated: true, completion: nil)
        }else{
            present(FCLoginViewController(), animated: true, completion: nil)
        }
        
    }
    func scanViewController(_ scanViewController: GetScanViewController, didFinishedGetCode code: String?, orderID: String?) {
        print(code ?? "code nil")
        view.bringSubview(toFront: scanResultView)
        scanResultView.showView(code: code)
    }
    
    
    func fileUpload(_ fileView: FilePreView, gotoFileView file: Any?) {
        gotoDocument()
    }
    func fileUpload(_ fileView: FilePreView, askBeforeExit alert: UIAlertController) {
        present(alert, animated: true, completion: nil)
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
        print("segue")
    }
    */
    func onReq(_ req: QQBaseReq!) {
        
    }
    func onResp(_ resp: QQBaseResp!) {
        
    }
    /**
     处理QQ在线状态的回调
     */
    public func isOnlineResponse(_ response: [AnyHashable : Any]!) {
        
    }

}
